package com.ruoyi.electricity.mapper;

import java.util.Date;
import java.util.List;

import com.ruoyi.electricity.domain.DTO.ElectricityByArea;
import com.ruoyi.electricity.domain.DTO.RunStatusDeviceDetection;
import com.ruoyi.electricity.domain.ElectricityData;
import com.ruoyi.electricity.domain.TElectricityData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用电Mapper接口
 *
 * @author ruoyi
 * @date 2023-10-28
 */
public interface TElectricityDataMapper {
    /**
     * 查询用电
     *
     * @param id 用电ID
     * @return 用电
     */
    public TElectricityData selectTElectricityDataById(String id);

    /**
     * 查询用电列表
     *
     * @param tElectricityData 用电
     * @return 用电集合
     */
    public List<TElectricityData> selectTElectricityDataList(TElectricityData tElectricityData);

    /**
     * 新增用电
     *
     * @param tElectricityData 用电
     * @return 结果
     */
    public int insertTElectricityData(TElectricityData tElectricityData);

    /**
     * 修改用电
     *
     * @param tElectricityData 用电
     * @return 结果
     */
    public int updateTElectricityData(TElectricityData tElectricityData);

    /**
     * 删除用电
     *
     * @param id 用电ID
     * @return 结果
     */
    public int deleteTElectricityDataById(String id);

    /**
     * 批量删除用电
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteTElectricityDataByIds(String[] ids);

    @Select("SELECT fieldName AS area, SUM(mElectric) AS electricity " +
            " FROM t_electricity_data " +
            " WHERE delFlag = '0' " +
            " AND insertTime BETWEEN #{startTime} AND #{endTime} " +
            " GROUP BY fieldName ")
    List<ElectricityByArea> getElectricityByAreaWithinTime(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Select("SELECT equipName," +
            "       electric," +
            "       hElectric," +
            "       dElectric," +
            "       mElectric," +
            "       yElectric," +
            "       CONCAT(TIMESTAMPDIFF(HOUR, STR_TO_DATE(runStartTime, '%Y-%m-%d %H:%i:%s'), NOW()), '.', " +
            "              (TIMESTAMPDIFF(MINUTE, STR_TO_DATE(runStartTime, '%Y-%m-%d %H:%i:%s'), NOW()) % 60)) AS hour," +
            "       electricityStatus" +
            " FROM t_electricity_data " +
            " where equipName is not null " +
            " GROUP BY equipName,electricityStatus")
    List<RunStatusDeviceDetection> getElectricityData();

    @Select("SELECT DISTINCT fieldName AS area FROM t_electricity_data ")
    List<String> selectAllArea();
}
